package com.ailanyin.modules.flowable.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.pagehelper.PageHelper;
import com.ailanyin.common.base.domain.BaseQuery;
import com.ailanyin.common.base.domain.Query;
import com.ailanyin.common.base.result.AjaxResult;
import com.ailanyin.common.database.result.CommonPage;
import com.ailanyin.common.log.annotation.Log;
import com.ailanyin.common.log.enums.BusinessType;
import com.ailanyin.common.poi.utils.ExcelUtil;
import com.ailanyin.modules.flowable.domain.WfCategory;
import com.ailanyin.modules.flowable.service.IWfCategoryService;

/**
 * 流程分类Controller
 *
 * @author ailanyin
 * @date 2022-12-09
 */
@Api(tags = "流程分类管理")
@RestController
@RequestMapping("/flowable/category")
public class WfCategoryController {

    @Autowired
    private IWfCategoryService wfCategoryService;

    @PreAuthorize("hasAuthority('flowable:category:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询列表('flowable:category:list')")
    public CommonPage list(WfCategory wfCategory,
                           @RequestParam(value = "pageSize" , defaultValue = "5") Integer pageSize,
                           @RequestParam(value = "pageNum" , defaultValue = "1") Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<WfCategory> list = wfCategoryService.selectList(wfCategory);
        return CommonPage.restPage(list);
    }

    @PreAuthorize("hasAuthority('flowable:category:export')")
    @Log(title = "流程分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出('flowable:category:export')")
    public void export(HttpServletResponse response, WfCategory wfCategory) {
        List<WfCategory> list = wfCategoryService.selectList(wfCategory);
        ExcelUtil.exportExcel(list, WfCategory.class, "流程分类数据" , response);
    }

    @PreAuthorize("hasAuthority('flowable:category:query')")
    @GetMapping(value = "/{categoryId}")
    @ApiOperation(value = "获取详情('flowable:category:query')")
    public AjaxResult getInfo(@PathVariable("categoryId") Long categoryId) {
        return AjaxResult.success(wfCategoryService.selectByCategoryId(categoryId));
    }

    @PreAuthorize("hasAuthority('flowable:category:add')")
    @Log(title = "流程分类", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增('flowable:category:add')")
    public AjaxResult add(@RequestBody WfCategory wfCategory) {
        return AjaxResult.toAjax(wfCategoryService.insert(wfCategory));
    }

    @PreAuthorize("hasAuthority('flowable:category:edit')")
    @Log(title = "流程分类", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改('flowable:category:edit')")
    public AjaxResult edit(@RequestBody WfCategory wfCategory) {
        return AjaxResult.toAjax(wfCategoryService.update(wfCategory));
    }

    @PreAuthorize("hasAuthority('flowable:category:remove')")
    @Log(title = "流程分类", businessType = BusinessType.DELETE)
	@DeleteMapping
    @ApiOperation(value = "删除('flowable:category:remove')")
    public AjaxResult remove(@RequestBody BaseQuery<Query<Long>> query) {
        return AjaxResult.toAjax(wfCategoryService.deleteBatch(query.getIds()));
    }
}
